﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

namespace Catering.Catering_Page.Caipin
{
    public partial class PurchaseInfo_Regth : UserIsNull
    {
            #region 申明
            Catering.View_Operaction_Common voc = new View_Operaction_Common();
            Catering.View_Operaction.Caipin_Manage_Operaction cmo = new Catering.View_Operaction.Caipin_Manage_Operaction();
            public string str_All = string.Empty;
            public string str_1 = string.Empty;
            public string str_2 = string.Empty;
            public string str_3 = string.Empty;
            public string str_4 = string.Empty;
            public string str_5 = string.Empty;
            public string str_6 = string.Empty;
            public string str_7 = string.Empty;
            public Int32 RadionID = 5;
            #endregion

            #region 页面加载
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    string str_backurl = Session["backurl"] as string;
                    voc.DropDownListDataBind_Group_tedian(this.dr_Group_Tedian);
                    //添加开始
                    if (Session["radioID"] != null)
                    {
                        string str = Session["radioID"] as string;
                        str = str.Replace("'", "").Replace("'", "").Trim();
                        Boolean bl = Int32.TryParse(str, out RadionID);
                        //添加结束
                        if (str_backurl != null)
                        {
                            string[] strs = str_backurl.Replace("'", "").Split(new char[] { '|', ' ' });
                            if (Session["Personal"] != null)
                            {
                                Getload_Personal(strs);
                                voc.DropDownListDataBind_zhou(this.dr_zhous, str_backurl);
                                GetDataList(RadionID);

                            }
                        }


                      
                    }
                    ViewState["caipu"] = cmo.GetShipuList(Session["GroupID"].ToString(), Session["ClassID"].ToString(), RadionID);
                }
            }
            #endregion

            #region 辅助
          
            public void Getload_Personal(string[] strs)
            {
                string str_Personal = Session["Personal"] as string;
                Catering_Rules.PersonalService crps = new Catering_Rules.PersonalService();
                Catering_Model.PersonalService cmpc = new Catering_Model.PersonalService();
                if (Session["Personal"] != null)
                {
                    cmpc = crps.GetModel(Convert.ToInt32(str_Personal.Replace("'", "").Replace("'", "")));
                }
                else
                {
                    cmpc =crps.GetModel(Convert.ToInt32(HFshipuid.Value));
                }
                Catering_Model.Group_tedian cmgt = new Catering_Rules.Group_tedian().GetModel(Convert.ToInt32(cmpc.temp2) < 1 ? 1 : Convert.ToInt32(cmpc.temp2));
                Catering_Model.MealTypes cmmt = new Catering_Rules.MealTypes().GetModel(Convert.ToInt32(cmpc.MealTypeID) < 1 ? 1 : Convert.ToInt32(cmpc.MealTypeID));
                lbl_Renqun.Text = cmpc.CateringName;
                if (strs.Length > 2)
                {
                    lbl_shijian.Text = strs[0] + "至" + strs[strs.Length - 2];
                }
                else
                {
                    lbl_shijian.Text = strs[0] + "至" + strs[1];
                }
                lblshanshi.Text = cmmt.MealType;
                lblpeican.Text = cmgt.PName;
            }
            #endregion

            #region 页面加载提供
            public void GetDataList(int weeklength)
            {

                string str_backurl = Session["backurl"] as string;

                string str_Personal = Session["Personal"] as string;
                if (str_backurl != null)
                {
                    string[] strs = str_backurl.Replace("'", "").Split(new char[] { '|', ' ' });
                    if (strs != null)
                    {
                        string str = "FMID=1";
                       int i = Convert.ToInt32(this.dr_zhous.Items[0].Value);

                        DataTable dt = voc.GetshipuInfo(strs[i], str, weeklength);
                        str_All = voc.Getshipu(strs[i], weeklength, dt);
                        ViewState["caipunnow"] = str_All;
                        Session["RecipeInformation"] = null;
                      //  Session["RecipeInformation"] = dt;
                        //DataTable dt = voc.GetshipuInfo(strs[i]);
                        //str_All = voc.Getshipu(strs[i], 7, dt);

                        str_1 = voc.Getshipu(strs[i]);
                        str_2 = voc.Getshipu(strs[i + 1]);
                        str_3 = voc.Getshipu(strs[i + 2]);
                        str_4 = voc.Getshipu(strs[i + 3]);
                        str_5 = voc.Getshipu(strs[i + 4]);
                        //修改
                        switch (weeklength)
                        {
                            case 6:
                                str_6 = voc.Getshipu(strs[i + 5]);
                                break;
                            case 7:
                                str_6 = voc.Getshipu(strs[i + 5]);
                                str_7 = voc.Getshipu(strs[i + 6]);
                                break;
                        }
                        //修改

                    }
                }

            }
            #endregion

            #region 按钮事件
            #region 选择第几周
            protected void Dr_zhoulist_SelectedIndexChanged(object sender, EventArgs e)
            {
                string str = "MID=1";
                if (Session["backurl"] != null)
                {

                    string str_backurl = Session["backurl"] as string;
                    string str_Personal = Session["Personal"] as string;
                    string[] strs = str_backurl.Replace("'", "").Split(new char[] { '|', ' ' });
                    if (strs.Length > 0)
                    {

                        int i = 1;// Convert.ToInt32(this.Dr_zhoulist.SelectedValue) * RadionID;
                        DataTable dt = voc.GetshipuInfo(strs[i], str, RadionID);
                        str_All = voc.Getshipu(strs[i], RadionID, dt);
                        str_1 = voc.Getshipu(strs[i],dt);
                        str_2 = voc.Getshipu(strs[i + 1], dt);
                        str_3 = voc.Getshipu(strs[i + 2], dt);
                        str_4 = voc.Getshipu(strs[i + 3], dt);
                        str_5 = voc.Getshipu(strs[i + 4], dt);
                        //修改
                        switch (RadionID)
                        {
                            case 6:
                                str_6 = voc.Getshipu(strs[i + 5],dt);
                                break;
                            case 7:
                                str_6 = voc.Getshipu(strs[i + 5], dt);
                                str_7 = voc.Getshipu(strs[i + 6], dt);
                                break;
                        }
                        //修改
                    }


                }
                if (HFshipuid.Value.Length > 0)
                {
                    int i = 1;// Convert.ToInt32(this.Dr_zhoulist.SelectedValue) * RadionID;
                    DateTime dtime = new Catering_Rules.T_Recipes().GetModel(Convert.ToInt32(HFshipuid.Value)).StateDate.AddDays(i);
                    DataTable dt = voc.GetshipuInfo(dtime.ToShortDateString(), str, RadionID);
                    str_All = voc.Getshipu(dtime.ToShortDateString(), RadionID, dt);
                    str_1 = voc.Getshipu(dtime.ToShortDateString(), dt, RadionID);
                    str_2 = voc.Getshipu(dtime.AddDays(1).ToShortDateString(), dt, RadionID);
                    str_3 = voc.Getshipu(dtime.AddDays(2).ToShortDateString(), dt, RadionID);
                    str_4 = voc.Getshipu(dtime.AddDays(3).ToShortDateString(), dt, RadionID);
                    str_5 = voc.Getshipu(dtime.AddDays(4).ToShortDateString(), dt, RadionID);

                    //修改
                    switch (RadionID)
                    {
                        case 6:
                            str_6 = voc.Getshipu(dtime.AddDays(5).ToShortDateString(), dt, RadionID);

                            break;
                        case 7:
                            str_6 = voc.Getshipu(dtime.AddDays(5).ToShortDateString(), dt, RadionID);
                            str_7 = voc.Getshipu(dtime.AddDays(6).ToShortDateString(), dt, RadionID);
                            break;
                    }
                    //修改

                }
            }
            #endregion

            #region 保存食谱
            protected void but_Save_Click(object sender, EventArgs e)
            {
                List<Catering_Model.E_RecipeInformation> list = Session["RecipeInformation"] as List<Catering_Model.E_RecipeInformation>;
                Catering_Rules.T_RecipeInformation crri = new Catering_Rules.T_RecipeInformation();
                Catering_Rules.T_Recipes crr = new Catering_Rules.T_Recipes();
                Catering_Model.E_Recipes cmr = new Catering_Model.E_Recipes();

                string str_group = Session["Group"] as string;
                Catering_Rules.GroupCatering crgc = new Catering_Rules.GroupCatering();
                Catering_Model.GroupCatering cmgc = crgc.GetModel(Convert.ToInt32(str_group.Replace("'", "").Replace("'", "")));
                cmr.CateringName = cmgc.GCName;
                cmr.ClassID = cmgc.ClaassID;
                cmr.GroupID = Convert.ToInt32(str_group.Replace("'", "").Replace("'", ""));
                cmr.PID = 1;
                cmr.Number = cmgc.MNumber + cmgc.WNumber;
                cmr.AreaID = cmgc.AreaID;
                cmr.ClassIDs = cmgc.ClaassID;
                cmr.Back = DateTime.Now.ToShortDateString();
                cmr.temp1 = "1";
                cmr.temp2 = "1";
                string str_backurl = Session["backurl"] as string;
                string[] strs = str_backurl.Replace("'", "").Split(new char[] { '|', ' ' });
                cmr.StateDate = Convert.ToDateTime(strs[0]);
                cmr.StopDate = Convert.ToDateTime(strs[strs.Length - 2]);
                cmr.EditPersonnelID = LoginPersonnel.id;
                cmr.EditTime = DateTime.Now;
                crr.Add(cmr);
                foreach (Catering_Model.E_RecipeInformation cmpi in list)
                {
                    cmpi.MID = cmr.id;
                    int NoWeek = 0;
                    NoWeek = new Catering_Rules.T_RecipesWeek().GetNumber(cmpi.Date, cmpi.MID);
                    cmpi.temp1 = NoWeek.ToString();
                    crri.Add(cmpi);
                }
                ImportPageMuber(cmr.id);
            }
            #endregion
           
            #region 自动生成食谱
            protected void but_input_shipuku_Click(object sender, EventArgs e)
            {
                DataTable dt = voc.GetshipuInfo(Session["GroupID"].ToString(), Session["ClassID"].ToString(),  RadionID,this.lblpeican.Text.Trim());
                DateTime dtime = Convert.ToDateTime(this.txtstatic_zidingyi.Text.Trim());
                ///将生成的食谱存放在Seesion
                Session["RecipeInformation"] = null;
                Session["RecipeInformation"] = voc.GetTableToList(dt, dtime);
                ///
                str_All = voc.Getshipu(dtime.ToShortDateString(), RadionID, dt);
                str_1 = voc.Getshipu(dtime.ToShortDateString(), dt);
                str_2 = voc.Getshipu(dtime.AddDays(1).ToShortDateString(), dt);
                str_3 = voc.Getshipu(dtime.AddDays(2).ToShortDateString(), dt);
                str_4 = voc.Getshipu(dtime.AddDays(3).ToShortDateString(), dt);
                str_5 = voc.Getshipu(dtime.AddDays(4).ToShortDateString(), dt);
                //修改
                switch (RadionID)
                {
                    case 6:
                        str_6 = voc.Getshipu(dtime.AddDays(5).ToShortDateString(), dt);
                        break;
                    case 7:
                        str_6 = voc.Getshipu(dtime.AddDays(5).ToShortDateString(), dt);
                        str_7 = voc.Getshipu(dtime.AddDays(6).ToShortDateString(), dt);
                        break;
                }
            }
            #endregion

            #region 当前食谱
            protected void but_now_shipu_Click(object sender, EventArgs e)
            {
                string[] str = this.lbl_shijian.Text.Trim().Split(new char[] { '至', ' ' });
                if (str.Length > 1)
                {
                   
                    DataTable dt = voc.GetshipuInfo(str[0], "MID = 1", RadionID);
                    ViewState["caipunnow"] = voc.Getshipu(str[0], RadionID, dt);
                    ViewState["caipunnowstat"] = "1--";

                }

            }
            #endregion

            #region 导入食谱
            protected void but_input_Click(object sender, EventArgs e)
            {
                string str_backurl = Session["backurl"] as string;
                if (Session["backurl"] != null)
                {
                    string[] strs = str_backurl.Replace("'", "").Split(new char[] { '|', ' ' });
                    Getload_Personal(strs);
                    ImportPageMuber(Convert.ToInt32(HFshipuid.Value));
                }
                else
                {
                    Catering_Model.E_Recipes cmr = new Catering_Rules.T_Recipes().GetModel(Convert.ToInt32(HFshipuid.Value));
                    string[] strs = new string[50];
                    strs[0] = cmr.StateDate.ToShortDateString();
                    strs[1] = cmr.StopDate.ToShortDateString();
                    Getload_Personal(strs);
                    ImportPageMuber(Convert.ToInt32(HFshipuid.Value));
                }
            }
            
            private void ImportPageMuber(int Recipes)
            {
                Catering.View_Operaction.Caipin_Manage_Operaction cmo = new View_Operaction.Caipin_Manage_Operaction();
                ArrayList list = new ArrayList();
                if (Recipes > 0)
                {

                    Catering_Model.E_Recipes cmr = new Catering_Rules.T_Recipes().GetModel(Recipes);
                    DateTime dtt = cmr.StateDate;
                    
                    string str = "MID=" + Recipes;
                    DataTable dt = voc.GetshipuInfo(dtt.ToShortDateString(), str, RadionID);
                    ViewState["caipunnow"] = dt;
                    ///将生成的食谱存放在Seesion
                    Session["RecipeInformation"] = null;
                    Session["RecipeInformation"] = voc.GetTableToList(dt, dtt);
                    ///
                    str_All=voc.Getshipu(dtt.ToShortDateString(), RadionID, dt);
                    str_1 =voc.Getshipu(dtt.ToShortDateString(), dt);
                    str_2 =voc.Getshipu(dtt.AddDays(1).ToShortDateString(), dt);
                    str_3 =voc.Getshipu(dtt.AddDays(2).ToShortDateString(), dt);
                    str_4 =voc.Getshipu(dtt.AddDays(3).ToShortDateString(), dt);
                    str_5 =voc.Getshipu(dtt.AddDays(4).ToShortDateString(), dt);
                  
                    //修改
                    switch (RadionID)
                    {
                        case 6:
                         
                            str_6 = voc.Getshipu(dtt.AddDays(5).ToShortDateString(), dt);
                  
                            break;
                        case 7:
                        
                             str_6 = voc.Getshipu(dtt.AddDays(5).ToShortDateString(), dt);
                    str_7 = voc.Getshipu(dtt.AddDays(6).ToShortDateString(), dt);
                            break;
                    }
                  
                }
            }

        
            #endregion
            #endregion


   
    }
}